#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2023-11-25 20:12
# @Author : hard work
# @File    : renix_create_ethII_traffic.py
# @Description :
from renix_py_api.api_gen import StreamTemplate, CreateHeaderCommand, UpdateHeaderCommand


from traffic_tool.tool_renix.renix_api.renix_base import Renix_base, renix_info_log


def create_l2_stream(port, pkt_len):
    stream = StreamTemplate(upper=port)
    assert stream.handle
    create_stream_header_cmd = CreateHeaderCommand(stream.handle, ['Ethernet.ethernetII'])
    create_stream_header_cmd.execute()
    stream.FixedLenth = pkt_len
    stream.get()
    return stream


def edit_stream(stream, param):
    update_header_cmd = UpdateHeaderCommand(Stream=stream.handle, Parameter=param)
    update_header_cmd.execute()
    ret = stream.get_children()[0]
    print(ret.__dict__)


class Create_ethII_traffic(Renix_base):
    """create l2 stream """

    def __init__(self):
        super().__init__()
        self.hVlan = None
        self.hEthernet = None
        self.hStreamBlock = None
        self.srcMac_pkt_cnt = None
        self.dstMac_pkt_cnt = None
        self.FixedLoad = None
        self.Duration = None
        self.BurstSize = None
        self.BURSTS = None
        self.frameSize = '128'
        self.dstMac = '00:00:00:00:00:01'
        self.srcMac = '00:0C:25:3B:05:F3'
        self.vid = None
        self.type = None
        self.prio = None
        self.hGeneratorConfig = None
        self.CrcError = None
        self.DurationMode = None

    def build_EthIILayer(self):
        renix_info_log('build_EthIILayer')
        stream1 = create_l2_stream(self.Port_1, self.frameSize)
        # some stream added to here if you need
        # stream2 = create_l2_stream(self.Port_2, '128')
        edit_stream(stream1, 'ethernetII_1.sourceMacAdd=''00:0C:25:3B:05:F3'' '
                             'ethernetII_1.destMacAdd=00:00:00:00:00:01' )

        # build_eth = BuildInterfaceCommand(InterfaceList=self.interface1, NetworkLayers='eth')
        # build_eth.execute()
        # eth_layer = self.interface1.get_children('EthIILayer')[0]
        # eth_layer.edit(sourceMacAdd='00:0C:25:3B:05:F3', dstMacAdd='00:00:00:00:00:01')

    def build_VlanLayer(self):
        renix_info_log('create EthII vlan Layer')
        stream1 = create_l2_stream(self.Port_1, self.frameSize)
        # some stream added to here if you need
        # stream2 = create_l2_stream(self.Port_2, '128')
        edit_stream(stream1, 'ethernetII_1.sourceMacAdd=00:30:6e:b3:f5:98 '
                             'ethernetII_1.destMacAdd=00:08:02:d7:80:16 '
                             'ethernetII_1.protocolType=8100 ethernetII_1.vlan '
                             'vlan_1.id=2 vlan_1.priority=2')

        # build_vlan = BuildInterfaceCommand(InterfaceList=self.interface1, NetworkLayers=['eth', 'vlan'])
        # build_vlan.execute()
        # vlan_layer = self.interface1.get_children('VlanLayer')[0]
        # vlan_layer.edit(VlanId=1, Step=1, Count=10)

    def build_EthII_by_StreamTemplate(self):
        stream = StreamTemplate(upper=self.Port_1)
        stream.edit(FixedLength=256, PayloadValue=88)
